package guda.shop.cms.manager.impl;

import guda.shop.cms.dao.PopularityGroupDao;
import guda.shop.cms.entity.PopularityGroup;
import guda.shop.cms.manager.PopularityGroupMng;
import guda.shop.cms.manager.ProductMng;
import guda.shop.common.hibernate3.Updater;
import guda.shop.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class PopularityGroupMngImpl
        implements PopularityGroupMng {
    private PopularityGroupDao popularityGroupDao;

    @Autowired
    private ProductMng productMng;

    @Transactional(readOnly = true)
    public Pagination getPage(int paramInt1, int paramInt2) {
        Pagination localPagination = this.popularityGroupDao.getPage(paramInt1, paramInt2);
        return localPagination;
    }

    @Transactional(readOnly = true)
    public PopularityGroup findById(Long paramLong) {
        PopularityGroup localPopularityGroup = null;
        if (paramLong != null)
            localPopularityGroup = this.popularityGroupDao.findById(paramLong);
        return localPopularityGroup;
    }

    public PopularityGroup save(PopularityGroup paramPopularityGroup) {
        this.popularityGroupDao.save(paramPopularityGroup);
        return paramPopularityGroup;
    }

    public PopularityGroup update(PopularityGroup paramPopularityGroup) {
        Updater localUpdater = new Updater(paramPopularityGroup);
        PopularityGroup localPopularityGroup = this.popularityGroupDao.updateByUpdater(localUpdater);
        return localPopularityGroup;
    }

    public PopularityGroup deleteById(Long paramLong) {
        PopularityGroup localPopularityGroup = findById(paramLong);
        localPopularityGroup.getProducts().clear();
        this.popularityGroupDao.deleteById(paramLong);
        return localPopularityGroup;
    }

    public PopularityGroup[] deleteByIds(Long[] paramArrayOfLong) {
        PopularityGroup[] arrayOfPopularityGroup = new PopularityGroup[paramArrayOfLong.length];
        int i = 0;
        int j = paramArrayOfLong.length;
        while (i < j) {
            arrayOfPopularityGroup[i] = deleteById(paramArrayOfLong[i]);
            i++;
        }
        return arrayOfPopularityGroup;
    }

    public void addProduct(PopularityGroup paramPopularityGroup, Long[] paramArrayOfLong) {
        if (paramArrayOfLong != null) {
            Long[] arrayOfLong = paramArrayOfLong;
            int i = arrayOfLong.length;
            for (int j = 0; j < i; j++) {
                long l = arrayOfLong[j].longValue();
                paramPopularityGroup.addToProducts(this.productMng.findById(Long.valueOf(l)));
            }
        }
    }

    public void updateProduct(PopularityGroup paramPopularityGroup, Long[] paramArrayOfLong) {
        paramPopularityGroup.getProducts().clear();
        if (paramArrayOfLong != null) {
            Long[] arrayOfLong = paramArrayOfLong;
            int i = arrayOfLong.length;
            for (int j = 0; j < i; j++) {
                long l = arrayOfLong[j].longValue();
                paramPopularityGroup.addToProducts(this.productMng.findById(Long.valueOf(l)));
            }
        }
    }

    @Autowired
    public void setDao(PopularityGroupDao paramPopularityGroupDao) {
        this.popularityGroupDao = paramPopularityGroupDao;
    }
}

